package itesm.coler.service;

import java.sql.SQLException;
import java.util.ArrayList;

import itesm.coler.data.DAOException;
import itesm.coler.data.DAOFactory;
import itesm.coler.data.ProblemDAO;
import itesm.coler.models.Problem;
import itesm.coler.models.Team;
import itesm.coler.models.User;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Produces({"application/xml","application/json"})
@Path("/problems")
public class ProblemResource {
	
	DAOFactory MySQLFactory = DAOFactory.getDAOFactory(DAOFactory.MySQL);
	ProblemDAO problemDAO = MySQLFactory.getProblemDAO();
	
	@GET
	public ArrayList<Problem> getProblems(){
		try {
			ArrayList<Problem> arregloProblems = problemDAO.findAll();
			return arregloProblems;
		} catch (DAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	@PUT
	@Path("/insert")
	@Produces("text/plain")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response insert(Problem problema){
		problemDAO.insertProblem(problema);
		problemDAO.insertWordtoProblem(problema);
		String result = "done";
		return Response.status(201).entity(result).build();
	}
	
	@PUT
	@Path("/modify")
	@Produces("text/plain")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response modify(Problem problema){
		problemDAO.modifyProblem(problema);
		String result = "done";
		return Response.status(201).entity(result).build();
	}
	
	@DELETE
	@Path("/delete/{problemName}")
	@Produces("text/plain")
	@Consumes(MediaType.APPLICATION_JSON)
	public Response delete(@PathParam("problemName") String id){
		
		problemDAO.deleteProblem(id);
		String result = "done";
		return Response.status(201).entity(result).build();
	}
	
	@GET
	@Path("/course/{id}")
	@Produces({"application/json","application/xml"})
	public ArrayList<Problem> getByCourse(@PathParam("id") String courseid){
		ArrayList<Problem> problems =problemDAO.findByCourse(courseid);
		return problems;
	}
	
	@GET
	@Produces({ "application/xml", "application/json" })
	@Path("/problem/{problemId}/")
	public Problem getProblem(@PathParam("problemId") int problemId) {
		Problem problem = problemDAO.findProblemById(problemId);
		return problem;
	}
	
}
